var geoHazardQueryForSketchQueryDlg=null;
var sketchQueryDlg =null;

function initSketchQueryDlg(){
	if(geologicHazardQueryDlg){
		geologicHazardQueryDlg.close();
		geologicHazardQueryDlg =null;
	}
	if(newSketchDig)
	{
		newSketchDig.close();
		newSketchDig=null;
	}
	if(global_vector_layer)
	{
		global_vector_layer.removeAllFeatures();
		global_vector_layer =null;
	}
	global_features_add = new Array();
	global_features_del = new Array();
	initGeoHazardDlg(); 
}

function initGeoHazardDlg(){
	if(geoHazardQueryForSketchQueryDlg)
		return;
	var geologicHazardDlgHtml = '<div style="width:340px;height:200px;font-size:11px;font-family:微软雅黑;position:absolute;left:15px;top:40px;">'+
												'<div id="yjdc3jbxxQueryConditionBar" style="position:absolute;left:0px;top:0px;width:340px;height:40px;">'+
												'<label>时间范围：</label><input id="yjdc3jbxxTimeInputForm" type="text" style="width:120px;height:18px;" class="Wdate" onfocus="WdatePicker({startDate: \'00:00:00\',dateFmt:\'yyyy-MM-dd HH:mm:ss\',alwaysUseStartDate:true})"/> '+
												' — <input id="yjdc3jbxxTimeInputTo" style="width:120px;height:18px;" type="text" class="Wdate" onfocus="WdatePicker({startDate:\'00:00:00\',dateFmt:\'yyyy-MM-dd HH:mm:ss\',alwaysUseStartDate:true})"/>'+
												'<br/>'+
												'<label>灾害名称：</label><input id="yjdc3jbxxNameInputTo" style="width:150px;height:15px;"/>'+
												'<input id="geoQueryBtn" type="button" value="确定" />'+
												'</div>'+
												'<div id="queryYjdc3JbxxResultContent" style="width:330px;height:135px;border:1px solid #ABADB3;position:absolute;left:2px;top:55px;overflow-y:auto;overflow-x:hidden;">'+
												'<table id="queryYjdc3JbxxResultTable" style="font-size:11px;width:330px;border-collapse: collapse;">'+
													'<tr>'+
														'<td class="queryTd">序号</td><td class="queryTd">灾害名称</td><td class="queryTd">灾点标识</td>'+
													'</tr>'+
												'</table>'+
											'</div>'+
											'</div>';
	geoHazardQueryForSketchQueryDlg = $.dialog({
		title: '基本情况查询',
		content: geologicHazardDlgHtml,
		dlgDivId: "geologicQueryDiv",
		resize: false,
		max: false,
		left:"250px",
		top:'100px',
		width:'340px',
		height:'200px',
		min:onGeoQueryDlgForSketchQueryMin,
		init:function(){
			$("#geoQueryBtn").click(onQueryGeoHazardForSketchQuery);
		},
		close:onGeoQueryDlgForSketchQueryClose
	});
	/*
	修改  20130505  增加对于灾点标识的修改
	*/
	function onQueryGeoHazardForSketchQuery(){
		var startTime = $("#yjdc3jbxxTimeInputForm").val();
		var endTime = $("#yjdc3jbxxTimeInputTo").val();
		var name = $("#yjdc3jbxxNameInputTo").val();
		var obj = {startTime:startTime,endTime:endTime,name:name};
		var url = "queryYjdc3jbxxBenji.shtml";
		$.post(url,obj,showGeoHazardDlgForSketchQuery);
	}
	
	function onGeoQueryDlgForSketchQueryClose(){
		geoHazardQueryForSketchQueryDlg = null;
	}
	
	function onGeoQueryDlgForSketchQueryMin(){
		minDlgByAnimate(geoHazardQueryForSketchQueryDlg);
	}
	
//	function onQueryGeoHazardForSketchQueryCallBack(result){
//		var result = eval("("+result+")");
//		//将查询到的结果，通过对话框展示出来
//		showGeoHazardDlgForSketchQuery(result);
//	}
	
	function showGeoHazardDlgForSketchQuery(res){
		$(".yjdc3ResultTr").remove();
		res = eval("("+res+")");
		if(res.length>0){
			for(var i = 0;i<res.length;i++){
				var tybh = res[i][0];
				var name = res[i][1];
				var zhType = getOperTypeZhdByCode(res[i][2]);
				var resStr =  '<tr class="yjdc3ResultTr">'+
				'<td class="queryTd">'+(i+1)+'</td><td class="queryClickTd" id="'+i+'" style="cursor:pointer;">'+name+'</td><td class="queryTd">'+zhType+'</td>'+
				'</tr>';
				$("#queryYjdc3JbxxResultTable").append(resStr);

			}
			$(".queryClickTd").click(function(evt){
				var index = evt.currentTarget.id;
				onClickOneHazardForSketchQuery(res[index][0],res[index][1],res[index][2]);
			});
		}else{
			alert("没有查询到任何信息!");
		}
	}
	function onClickOneHazardForSketchQuery(tybh,name,zdType){
		clickName = name;
		var url = "queryYjdc3jbxxByTybh.shtml";
		var startTime = $("#yjdc3jbxxTimeInputForm").val();
		var endTime = $("#yjdc3jbxxTimeInputTo").val();
		var data = {
				startTime:startTime,
				endTime:endTime,
				tybh:tybh,
				biaoshi:zdType
		};
		global_sketch_geoCode = tybh;
		global_sketch_geoCodeType = zdType;
		$.post(url,data,initSketchQueryDlg);
	}
	
	function initSketchQueryDlg(res){
		if(global_vector_layer){
			map.removeLayer(global_vector_layer);
			global_vector_layer = null;
		}
		if(geoHazardQueryForSketchQueryDlg){
			//geoHazardQueryForSketchQueryDlg=null;
		}
		res = eval("("+res+")");
		var sketchQueryDlgHTML= '<div style="width:340px;height:200px;font-size:11px;font-family:微软雅黑;position:absolute;left:15px;top:40px;">'+
								'<label style="color: red;">请您选择应急调查的时间:</label><select style="width:150px;" id="chooseTime"></select>'+
								'<div id="div_new_sketch" style="margin-top:60px;margin-left:10px">'+
									'<input type="button" value="添加线" style="margin-right:20px" id="btn_line_add">'+
									'<input type="button" value="添加点" style="margin-right:20px" id="btn_pnt_add">'+
							
									'<input type="button" value="删除" style="margin-right:20px" id="btn_del">'+
								'</div>'+
								'<div style="margin-top:50px">'+
									'<input type="button" value="保存" style="margin-left:260px" id="btn_newsketch_save">'+
								'</div>'+
								'</div>';
		sketchQueryDlg = $.dialog({
			title: '素描编辑',
			content: sketchQueryDlgHTML,
			dlgDivId: "sketchQueryDlgDiv",
			resize: false,
			max: false,
			left:"250px",
			top:'100px',
			width:'340px',
			height:'200px',
			min:onSketchQueryDlgMin,
			
			init:function(){
					$("#fanzaoDiv td").css("border","1px solid #D8DBE0");
					//初始化完毕，加载时间选择列表
					var optionStr = '<option selected="selected" value="">请选择调查时间</option>';
					
					for(var i = 0;i<res.length;i++){
						var time = res[i].yjdc01270;
						var year = Number(time.year)+1900;
						var month = (Number(time.month)+1)<10?"0"+(Number(time.month)+1):(Number(time.month)+1);
						var day = Number(time.date<10)?"0"+time.date:time.date;
						var hours = Number(time.hours<10)?"0"+time.hours:time.hours;
						var minutes = Number(time.minutes<10)?"0"+time.minutes:time.minutes;
						var seconds = Number(time.seconds<10)?"0"+time.seconds:time.seconds;
						var timeStr  = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
						optionStr = optionStr + '<option value="'+res[i].yjdc01270.time+'">'+timeStr+'</option>';
					}
					
					$("#chooseTime").html("");
					$("#chooseTime").change(queryAllFeaturesByTime);
					
					$("#btn_line_add").click(onAddLine);
					$("#btn_pnt_add").click(onAddPnt);
					$("#btn_del").click(onDeleteFeature);
					$("#btn_newsketch_save").click(onSaveForSketchQuery);
					
					$("#chooseTime").append(optionStr);

//					$("#chooseTime").change( function(){
//						var index = $("#chooseTime").val();
//						if(index=="")
//							return;
//						giveValueToEdit(res[index]);
//					})
					
					$("#editInsertButton").click(editInsertClickFunction);
					$("#geohazardBtn").click(onQueryGeoHazardFunction);
			},
			close:onSketchQueryDlgClose
		});
	}
}

    function queryAllFeaturesByTime(){
    	if(global_vector_layer)
    	{
    		global_vector_layer.removeAllFeatures();
    		global_vector_layer =null;
    	}
    	global_features_add = new Array();
    	global_features_del = new Array();
    	var time  = $("#chooseTime").val();
    	if(time == "")
    		return;
    	var data = {
    			time:time,
    			geoCode:global_sketch_geoCode,
    			geoCodeType:global_sketch_geoCodeType
    	}
    	$.post("query_sketch.shtml",data,onQueryAllFeaturesCallBack);
    }
    
    function onQueryAllFeaturesCallBack(res){
    	var obj = $.parseJSON(res);
    	if(!global_vector_layer){
    		global_vector_layer = new OpenLayers.Layer.Vector("SketchLayer");
    		map.addLayer(global_vector_layer);
    	}
    	for(var i=0;i<obj.length; i++){
    		var featureObj = obj[i];
    		var geometryObj = $.parseJSON(featureObj.sketchString);
    		var geometryString = geometryObj.geometry;
    		var type = geometryObj.type;
    		var feature;
    		if(type==0){
    			feature = getPoint(geometryString);
    		}
    		else{
    			feature = getLine(geometryString);
    		}
    		feature.fid=featureObj.id;
    		global_vector_layer.addFeatures([feature]);
    	}
    }
    
    function getLine(geometryString){
    	var xys = geometryString.split(',');
    	var pntList = new Array();
    	for(var i=0;i<xys.length;i++){
    		if(i%2==0){
    			var x= parseFloat(xys[i]);
        		var y = parseFloat(xys[i+1]);
        		var pnt = new OpenLayers.Geometry.Point(x,y);
        		pntList.push(pnt);
    		}
    	}
    	var line =  new OpenLayers.Geometry.LineString(pntList);
    	return new OpenLayers.Feature.Vector(line,null,null);
    }
    
    function getPoint(geometryString){
    	var xys = geometryString.split(',');
        var point = new OpenLayers.Geometry.Point(parseFloat(xys[0]),parseFloat(xys[1]));
        var pointFeature = new OpenLayers.Feature.Vector(point,null,null);
        return pointFeature;
    }
    
    function onSaveForSketchQuery(){
    	var geoTime=$("#chooseTime").val();
    	if( geoTime== ""){
    		alert("请选择一个时间");
    		return;
    	}
    	if(!global_vector_layer){
    		alert("请先绘制要素");
    		return;
    	}
    	var features = global_vector_layer.features;
    	var data={
    			geoCode:global_sketch_geoCode,
    			geoCodeType:global_sketch_geoCodeType,
    			time:geoTime,
    			add_features:JSON.stringify(global_features_add),
    			del_features:JSON.stringify(global_features_del)
    	}
    	var url ="save_or_del_sketch.shtml";
    	global_features_add = new Array();
    	global_features_del = new Array();
    	$.post(url,data,onPostDelAndAddSketch);
    }
    
    function onPostDelAndAddSketch(data){
    	if(data=="true"){
    		alert("添加成功");
    	}
    	else{
    			alert("添加失败");
    	}
    }
    
    function processFeatureToDataForSketchQuery(geoCode,geoTime){

    }
    
function onSketchQueryDlgMin(){
	minDlgByAnimate(sketchQueryDlg);
}

function onSketchQueryDlgClose(){
	if(global_vector_layer)
	{
		if(global_vector_layer.my_control){
			global_vector_layer.my_control.deactivate();
			map.removeControl(global_vector_layer.my_control);
		}
		global_vector_layer.removeAllFeatures();
		global_vector_layer =null;
	}
	sketchQueryDlg =null;
}
//////////////////////////////////
function onClickSketchQuery(tybh,name,zdType){
	debugger;
		var url = "queryYjdc3jbxxByTybh.shtml";
		var data = {
				startTime:"",
				endTime:"",
				tybh:tybh,
				biaoshi:zdType
		};
		global_sketch_geoCode = tybh;
		global_sketch_geoCodeType = zdType;
		$.post(url,data,initOnClickSketchQueryDlg);
	}
	
	function initOnClickSketchQueryDlg(res){
		debugger;
		if(global_vector_layer){
			map.removeLayer(global_vector_layer);
			global_vector_layer = null;
		}
		if(geoHazardQueryForSketchQueryDlg){
			//geoHazardQueryForSketchQueryDlg=null;
		}
		res = eval("("+res+")");
		var sketchQueryDlgHTML= '<div style="width:340px;height:200px;font-size:11px;font-family:微软雅黑;position:absolute;left:15px;top:40px;">'+
								'<label style="color: red;">请您选择应急调查的时间:</label><select style="width:150px;" id="chooseTime"></select>'+
								'</div>';
		sketchQueryDlg = $.dialog({
			title: '素描图',
			content: sketchQueryDlgHTML,
			dlgDivId: "sketchQueryDlgDiv",
			resize: false,
			max: false,
			left:"250px",
			top:'100px',
			width:'340px',
			height:'50px',
			min:onSketchQueryDlgMin,
			
			init:function(){
					$("#fanzaoDiv td").css("border","1px solid #D8DBE0");
					//初始化完毕，加载时间选择列表
					var optionStr = '<option selected="selected" value="">请选择调查时间</option>';
					
					for(var i = 0;i<res.length;i++){
						var time = res[i].yjdc01270;
						var year = Number(time.year)+1900;
						var month = (Number(time.month)+1)<10?"0"+(Number(time.month)+1):(Number(time.month)+1);
						var day = Number(time.date<10)?"0"+time.date:time.date;
						var hours = Number(time.hours<10)?"0"+time.hours:time.hours;
						var minutes = Number(time.minutes<10)?"0"+time.minutes:time.minutes;
						var seconds = Number(time.seconds<10)?"0"+time.seconds:time.seconds;
						var timeStr  = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
						optionStr = optionStr + '<option value="'+res[i].yjdc01270.time+'">'+timeStr+'</option>';
					}
					
					$("#chooseTime").html("");
					$("#chooseTime").change(queryAllFeaturesByTime);
					$("#chooseTime").append(optionStr);


					
					//$("#editInsertButton").click(editInsertClickFunction);
					///$("#geohazardBtn").click(onQueryGeoHazardFunction);
			},
			close:onSketchQueryDlgClose
		});
}

   
